/*
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.
 */
package org.apache.cordova;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaInterface;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.CordovaWebView;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;

/**
 * Network manager plugin for Desktop Cordova.
 * FIXME CJO - not finish at all
 * 
 * @author Cyril Joui
 */
public class NetworkManager extends CordovaPlugin {

	public static int NOT_REACHABLE = 0;
	public static int REACHABLE_VIA_CARRIER_DATA_NETWORK = 1;
	public static int REACHABLE_VIA_WIFI_NETWORK = 2;

	public static final String WIFI = "wifi";
	public static final String WIMAX = "wimax";
	// mobile
	public static final String MOBILE = "mobile";
	// 2G network types
	public static final String GSM = "gsm";
	public static final String GPRS = "gprs";
	public static final String EDGE = "edge";
	// 3G network types
	public static final String CDMA = "cdma";
	public static final String UMTS = "umts";
	public static final String HSPA = "hspa";
	public static final String HSUPA = "hsupa";
	public static final String HSDPA = "hsdpa";
	public static final String ONEXRTT = "1xrtt";
	public static final String EHRPD = "ehrpd";
	// 4G network types
	public static final String LTE = "lte";
	public static final String UMB = "umb";
	public static final String HSPA_PLUS = "hspa+";
	// return type
	public static final String TYPE_UNKNOWN = "unknown";
	public static final String TYPE_ETHERNET = "ethernet";
	public static final String TYPE_WIFI = "wifi";
	public static final String TYPE_2G = "2g";
	public static final String TYPE_3G = "3g";
	public static final String TYPE_4G = "4g";
	public static final String TYPE_NONE = "none";

	private CallbackContext connectionCallbackContext;
	/*
	private static final String LOG_TAG = "NetworkManager";

	private boolean registered = false;
*/
	/**
	 * Sets the context of the Command. This can then be used to do things like
	 * get file paths associated with the Activity.
	 * 
	 * @param cordova
	 *            The context of the main Activity.
	 * @param webView
	 *            The CordovaWebView Cordova is running in.
	 */
	public void initialize(CordovaInterface cordova, CordovaWebView webView) {
		super.initialize(cordova, webView);
	}

	/**
	 * Executes the request and returns PluginResult.
	 * 
	 * @param action
	 *            The action to execute.
	 * @param args
	 *            JSONArry of arguments for the plugin.
	 * @param callbackContext
	 *            The callback id used when calling back into JavaScript.
	 * @return True if the action was valid, false otherwise.
	 */
	public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
		if (action.equals("getConnectionInfo")) {
			this.connectionCallbackContext = callbackContext;
			PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, this.getConnectionInfo());
			pluginResult.setKeepCallback(true);
			callbackContext.sendPluginResult(pluginResult);
			return true;
		}
		return false;
	}

	/**
	 * Get the latest network connection information
	 * 
	 * @param info
	 *            the current active network info
	 * @return a JSONObject that represents the network info
	 */
	private String getConnectionInfo() {
		// FIXME CJO - how to handle this on Desktop?
		return WIFI;
	}

}
